package com.example.yuntiku.Servlet;

import com.example.yuntiku.dao.impl.UsersDao;
import com.example.yuntiku.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UpdatePwdServlet", value = "/UpdatePwdServlet")
public class UpdatePwdServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String uid = request.getParameter("email");
        UsersDao usersDao = new UsersDao();
        User users = usersDao.selectById(Integer.parseInt(uid));
        // 将简历信息保存到request中传递给页面
        request.setAttribute("users", users);
        // 转发到修改页面
        request.getRequestDispatcher("forgot_password.jsp").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String email = (String) request.getSession().getAttribute("email");
        request.setCharacterEncoding("UTF-8");
        // 获取修改后的简历信息
        String upwd = request.getParameter("upwd");
        // 创建一个Resume对象并设置修改后的字段值
        UsersDao usersDao = new UsersDao();
        User users = new User();
        users.setEmail(email);
        users.setUpwd(upwd);
        // 调用dao进行修改
        usersDao.update(users);
        // 重定向到首页或者其他页面
        response.sendRedirect("logon.jsp");
    }
}
